﻿
/****************************** 
 * 
 *  Create By: Arthur.li
 *  
 *  Create At: 2009年4月23日 9:39:48
 *  
 *  Description: SIM信息管理
 *  
 *  Remark: 
 *  
 * ****************************/


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using Jumpton.Persistent;
using Jumpton.SFA.Server.Entity;
using Jumpton.SFA.Server.BusinessRule;

public partial class System_Mobile_SIMManager : PageBase
{
    // 当前对该SIM的操作提示
    String _controlInfo = " -- {0}";


    protected void Page_Load(object sender, EventArgs e)
    {
       
        if (!IsPostBack)
        {
            CreateSIM();
            BindData();
        }
    }

    protected void btnOk_Click(object sender, EventArgs e)
    {
        if (txtIMSI.Value.Equals("") || txtPhone.Value.Equals(""))
        {
            ShowMessage(Resources.Mobile.HaveNotCompleteSIMInfo);
            return;
        }

        SIMInfo sim = null;
        SIMInfoManager manager = new SIMInfoManager();

        if (txtSelect.Value.Equals("")) // 当前为创建
        {
            // 判断是否已经存在
            if (manager.IsExists(txtIMSI.Value, txtPhone.Value))
            {
                ShowMessage(Resources.Mobile.SIMExists);
                return;
            }

            // 创建一个
            sim = new SIMInfo();
        }
        else
        {
            sim = manager.LoadByCode(txtSelect.Value);
        }

        sim.IMSICode = txtIMSI.Value;
        sim.IsActive = rbtnIsActive.Items[0].Selected;
        sim.PhoneNumber = txtPhone.Value;
        sim.UpdateBy = MyStateManager.Name;
        sim.Save();

        if (sim.ID > 0)
        {
            // 提示修改成功
            ShowMessage(Resources.Mobile.ModifySIMSuccess);
        }
        else
        {
            // 提示创建成功
            ShowMessage(Resources.Mobile.CreateSIMSuccess);
        }

        // 没有选择"继续创建", 或者当前为修改操作
        if (!chkMore.Visible || !chkMore.Checked || !txtSelect.Value.Equals(""))
        {
            chkMore.Visible = false;
            PreCreate(false);
            litInfo.Text = "";
        }

        Reset();
        BindData();
    }

    protected void btnReset_Click(object sender, EventArgs e)
    {
        Reset();
    }

    /// <summary>
    /// 重置移动设备文本框信息
    /// </summary>
    private void Reset()
    {
        txtIMSI.Value = txtPhone.Value = "";
        rbtnIsActive.Items[0].Selected = true;

        //txtSelect.Value = "";
    }

    /// <summary>
    /// 向设备列表中绑定数据
    /// </summary>
    protected void BindData()
    {
        //string condition = "isActive=1";
        //if (!txtSearch.Value.Equals(""))
        //{
        //    condition += String.Format(" and (IMSICode like '%{0}%' or PhoneNumber like '%{0}%')", txtSearch.Value);
        //}

        ObjectTable table = new SIMInfoManager().QuickSearch(txtSearch.Value);
        BindData(table);

    }

    /// <summary>
    /// 绑定数据到GridView
    /// </summary>
    /// <param name="source"></param>
    private void BindData(object source)
    {
        gvResult.DataSource = source;
        gvResult.DataBind();
       
    }

    protected void btnCreate_Click(object sender, EventArgs e)
    {
        CreateSIM();
    }

    /// <summary>
    /// 创建一个SIM信息
    /// </summary>
    private void CreateSIM()
    {
        Reset();

        chkMore.Visible = true;
        PreCreate(true);

        txtIMSI.Focus();

        // 提示正在创建
        litInfo.Text = String.Format(_controlInfo, Resources.SiteMap.CreateText);
    }

    /// <summary>
    /// 准备创建移动设备
    /// </summary>
    /// <param name="can"></param>
    private void PreCreate(Boolean can)
    {
        btnReset.Enabled = can;
        btnOk.Enabled = can;
    }

    protected void btnSearch_Click(object sender, EventArgs e)
    {
        BindData();
    }

    protected void gvResult_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            //e.Row.Cells[2].Text = GetOrganizationName(e.Row.Cells[2].Text);
            //e.Row.Cells[3].Text = GetPositionName(e.Row.Cells[3].Text);
        }
    }
    protected void gvResult_SelectedIndexChanged(object sender, EventArgs e)
    {
        // 提示正在修改
        litInfo.Text = String.Format(_controlInfo, Resources.SiteMap.ModifyText);

        SIMInfo simInfo = new SIMInfoManager().LoadByCode(gvResult.SelectedValue.ToString());

        if (simInfo == null)
        {
            return;
        }

        txtSelect.Value = simInfo.IMSICode;

        txtIMSI.Value = simInfo.IMSICode;
        txtPhone.Value = simInfo.PhoneNumber;
        txtUpdateBy.Value = simInfo.UpdateBy;
        txtUpdateTime.Value = simInfo.LastUpdate.ToString();
        if (simInfo.IsActive)
        {
            rbtnIsActive.Items[0].Selected = true ;
        }
        else
        {
            rbtnIsActive.Items[0].Selected = false;
        }

        PreCreate(true);
    }

    protected void PageIndexChanged(object sender, GridViewPageEventArgs e)
    {
        gvResult.PageIndex = e.NewPageIndex;
        BindData();
    }

}
